﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace View.BookForms
{
    public partial class BookEditForm : Form
    {
        BookEventArgs _properties;

        public BookEditForm()
        {
            InitializeComponent();
        }

        private void BookEditForm_Shown(object sender, EventArgs e)
        {
            numericUdEdition.Value = 1;
            numericUdYear.Maximum = DateTime.Now.Year;
            numericUdYear.Value = DateTime.Now.Year;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            Controls2Fields();
            OnSubmit();
            SafeClose();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            SafeClose();
        }

        private void SafeClose()
        {
            _properties = null;
            Hide();
        }

        public void ShowDialog(BookEventArgs librarianFields)
        {

            _properties = librarianFields;
            Fields2Controls();
            ShowDialog();
        }

        private void Controls2Fields()
        {
            _properties.Author = textBoxAuthor.Text;
            _properties.Title = textBoxTitle.Text;
            _properties.Publisher = textBoxPublisher.Text;
            _properties.Year = (ushort?) numericUdYear.Value;
            _properties.Edition = (ushort?) numericUdEdition.Value;
            _properties.Isbn = textBoxIsbn.Text;
        }

        private void Fields2Controls()
        {
            textBoxAuthor.Text = _properties.Author;
            textBoxTitle.Text = _properties.Title;
            textBoxIsbn.Text = _properties.Isbn;
            textBoxIsbn.Text = _properties.Isbn;
        }

        public event EventHandler<BookEventArgs> Submit;

        private void OnSubmit()
        {
            if (Submit != null)
            {
                Submit(this, _properties);
            }
        }

        private void BookEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

    }
}
